﻿using UnityEngine;
using System.Collections;

public class UIMaster : MonoBehaviour {
    public static UIMaster instance;

    public GameObject JoyStick;

    [HideInInspector]
    public float inputHorizontal;
    [HideInInspector]
    public Vector3 inputDirection;
    [HideInInspector]
    public bool isFirePressing;
    [HideInInspector]
    public bool isFireDown;
    [HideInInspector]
    public bool isFireUp;

    private JoyStick _joyStickReactive;

    void Awake()
    {
        instance = this;
    }

	void Start () {
        JoyStick.SetActive(false);
        _joyStickReactive = JoyStick.GetComponent<JoyStick>();
	}

    void Update()
    {
        if(Input.GetKeyDown(KeyCode.Escape))
        {
            Application.Quit();
        }

        isFireDown = Input.GetButtonDown("Fire1");
        isFirePressing = Input.GetButton("Fire1");
        isFireUp = Input.GetButtonUp("Fire1");
        
        bool hasRightTouch = false;
        Touch rightTouch = new Touch();
        Touch[] touchs = Input.touches;
        for (int i = 0; i < touchs.Length && i < 2; i++)
        {
            Touch t = touchs[i];
            if (!hasRightTouch && t.position.x > 900)
            {
                rightTouch = t;
                hasRightTouch = true;
            }
        }
        if(hasRightTouch)
        {
            isFireDown = rightTouch.phase == TouchPhase.Began;
            isFirePressing = rightTouch.phase == TouchPhase.Moved;
            isFireUp = rightTouch.phase == TouchPhase.Ended;
        }


        ///////////////////////////用鼠标操作方向///////////////////////////////////////

        if (Input.GetMouseButton(0) && _joyStickReactive != null)
        {
            Vector3 mousePos = Input.mousePosition;
           
            if (Input.GetMouseButtonDown(0))
            {
                JoyStick.transform.position = mousePos;
                JoyStick.SetActive(true);
            }
            else
            {
                _joyStickReactive.Accept(mousePos);
                //Debug.Log("joyStick output=" + _joyStickReactive.getHorizontalValue());
            }
        }
        if (Input.GetMouseButtonUp(0))
        {
            _joyStickReactive.Reset();
        }

        ////////////////////////////////////////////////////////////////////


#if UNITY_ANDROID
        bool hasLeftTouch = false;
        Touch leftTouch = new Touch();
        for(int i=0; i<touchs.Length && i<2; i++)
        {
            Touch t = touchs[i];
            if(!hasLeftTouch && t.position.x <= 900)
            {
                leftTouch = t;
                hasLeftTouch = true;
            }
        }

        if(hasLeftTouch && _joyStickReactive!=null)
        {
            Vector3 mousePos = leftTouch.position;
            if (leftTouch.phase == TouchPhase.Began)
            {
                JoyStick.transform.position = mousePos;
                JoyStick.SetActive(true);
                //_joyStickReactive.StartAccept();
            }
            else if(leftTouch.phase == TouchPhase.Moved)
            {
                _joyStickReactive.Accept(mousePos);
                //Debug.Log("joyStick output=" + _joyStickReactive.getHorizontalValue());
            }
            else if(leftTouch.phase == TouchPhase.Ended)
            {
                _joyStickReactive.Reset();
            }
        }
#endif

        inputHorizontal = _joyStickReactive.inputMoveVal;

        inputDirection = _joyStickReactive.inputDirection;
    }

}
